/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.token.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opgea.token.service.CompanyService;
import com.opgea.token.util.FacesUtil;
import com.opgea.token.web.dto.CompanyDTO;

/**
 * 
 * @author Ramesh
 */
@Component
@Scope(value = "request")
@ManagedBean(name = "companyController")
@RequestScoped
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    private CompanyDTO companyDTO = new CompanyDTO();
    private List<CompanyDTO> companyList = new ArrayList<CompanyDTO>();

    @PostConstruct
    public void init() {
	// long companyId = FacesUtil.getSessionUserDataObject().getCompanyId();
	companyList = companyService.findAll();
	// companyDTO = companyService.find(companyId);
    }

    public CompanyDTO getCompanyDTO() {
	return companyDTO;
    }

    public void setCompanyDTO(CompanyDTO companyDTO) {
	this.companyDTO = companyDTO;
    }

    public List<CompanyDTO> getCompanyList() {
	return companyList;
    }

    public void setCompanyList(List<CompanyDTO> companyList) {
	this.companyList = companyList;
    }

    public String save() {
	companyService.update(companyDTO);
	String message = null;
	if (companyDTO.getCompanyId() > 0) {
	    message = "Updated Successfully!";
	    FacesUtil.addMessage("messages", FacesMessage.SEVERITY_INFO,
		    message, null);
	    return "create.xhtml";
	} else {
	    message = "Login details is sent to your email account: "
		    + companyDTO.getEmail();
	    FacesUtil.addMessage("messages", FacesMessage.SEVERITY_INFO,
		    message, null);
	    return "new.xhtml";
	}
    }

    public String edit() {
	String companyId = (String) FacesUtil.getRequestParameter("companyId");
	companyDTO.setCompanyId(new Long(companyId));
	companyDTO = companyService.find(companyDTO.getCompanyId());
	return "create.xhtml";
    }
}
